package org.kinglone.back.loginLog.service;

import java.util.Date;

import javax.annotation.Resource;

import org.kinglone.back.loginLog.model.LoginLog;
import org.kinglone.back.loginLog.service.LoginLogService;
import org.kinglone.common.dao.impl.BaseDaoImpl;
import org.kinglone.common.util.DateUtils;
import org.kinglone.common.util.PageQuery;
import org.kinglone.common.util.PageResults;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("loginLogService")
@Transactional(readOnly=false)
public class LoginLogServiceImpl extends BaseDaoImpl<LoginLog> implements LoginLogService {
			
	@Resource(name="loginLogService")
	private LoginLogService LoginLogService;

	//保存登录状态
	@Override
	public void saveLoginLog(String remoteIP, String clientIP, String status,String username) {
		try {
			LoginLog loginLog = new LoginLog();
			loginLog.setClientIP(clientIP);
			loginLog.setRemoteIP(remoteIP);
			loginLog.setLoginTime(DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
			loginLog.setStatus(status);
			loginLog.setUsername(username);
			
			LoginLogService.save(loginLog);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	//查询登录日志
	@Override
	public PageResults<LoginLog> getLoginLog(PageQuery pageQuery) {
		
		String hql ="from LoginLog g order by g.loginTime desc";
		
		String countHql="select count(0) from LoginLog";
		
		PageResults<LoginLog> pageResults = super.findPageByFetchedHql(hql, countHql, pageQuery, null);
		
		return pageResults;
	}


	

}
